package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ArticleConfigMapper;
import com.heima.article.mapper.ArticleContentMapper;
import com.heima.article.mapper.ArticleMapper;
import com.heima.article.service.ArticleService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ApArticle> implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleContentMapper articleContentMapper;

    @Autowired
    private ArticleConfigMapper articleConfigMapper;



    /**
     * 加载首页-加载更多-加载更新 三位一体
     *
     * @param dto  type=1 认为是加载更多，type=2表示加载更新
     * @param type
     * @return
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto, Short type) {

        List<ApArticle> apArticles = articleMapper.loadArticleList(dto, type);
        //返回数据
        return ResponseResult.okResult(apArticles);
    }


    /**
     * 保存三剑客
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public ResponseResult save(ArticleDto dto) {
        //1. 复制dto给pojo
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);

        //2. id为空新增
        if (apArticle.getId() == null) {
            //2.1保存文章表数据
            this.save(apArticle);

            //2.2 保存文章配置表数据
            ApArticleConfig config = new ApArticleConfig();
            config.setArticleId(apArticle.getId());//设置文章id
            config.setIsComment(true);//默认开启评论
            config.setIsForward(true);//默认开启转发
            config.setIsDown(true);//默认未下架
            config.setIsDelete(false);//默认未删除
            articleConfigMapper.insert(config);

            //2.3 保存文章内容表数据
            ApArticleContent content = new ApArticleContent();
            content.setArticleId(apArticle.getId());
            content.setContent(dto.getContent());
            articleContentMapper.insert(content);
        } else {
            //3. id不为空修改
            //3.1 修改文章表数据
            this.updateById(apArticle);

            //3.2 修改内容表数据
            articleContentMapper.update(null, Wrappers.<ApArticleContent>lambdaUpdate()
                    .eq(ApArticleContent::getArticleId, apArticle.getId())
                    .eq(ApArticleContent::getContent, dto.getContent()));
        }
        //4.响应文章主表ID
        return ResponseResult.okResult(apArticle.getId());
    }

}
